/* eslint-disable no-console */
'use static';

class AliLogTracker {
  constructor(host, project, logstore) {
    this.uri_ = 'https://' + project + '.' + host + '/logstores/' + logstore + '/track?APIVersion=0.6.0';
    this.params_ = new Array();
  }
  push(key, value) {
    if (!key) {
      return;
    }
    if (value === '') {
      return;
    }
    this.params_.push(key);
    this.params_.push(value);
  }

  logger() {
    var url = this.uri_;
    var k = 0;
    while (this.params_.length > 0) {
      if (k % 2 == 0) {
        url += '&' + encodeURIComponent(this.params_.shift());
      } else {
        url += '=' + encodeURIComponent(this.params_.shift());
      }
      ++k;
    }
    try {
      fetch(url)
    } catch (ex) {
      if (window && window.console && typeof window.console.log === 'function') {
        console.log("Failed to log to ali log service because of this exception:\n" + ex);
        console.log("Failed log data:", url);
      }
    }
  }
}

export default AliLogTracker;